package com.codechuan.demo.ui.myMainToken;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.demo.R;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.dto.TokenObject;
import com.codechuan.demo.ui.login.LoginActivity;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.HttpControllerUtil;
import com.codechuan.demo.utils.MyHttpUtil;

public class MyMainToken extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_my_main_token);

        // 发送验证码
        Button sendCode = findViewById(R.id.main_sendCode);
        sendCode.setOnClickListener(this);

        // 获取Token
        Button getToken = findViewById(R.id.main_getToken);
        getToken.setOnClickListener(this);

        // 返回列表
        Button add_return = findViewById(R.id.add_main_return);
        add_return.setOnClickListener(this);

//        // 获取VideoView组件的引用
//        String videoUrl = "https://img.tukuppt.com/video_show/2405179/00/02/35/5b56c8b31b1ad_10s_big.mp4"; // 替换为实际的视频URL
//        Uri uri = Uri.parse(videoUrl);
//        VideoView videoView = findViewById(R.id.videoView);
//        videoView.setVideoURI(uri);
//        videoView.start();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (R.id.main_sendCode == id) {
            TextView add_msg = findViewById(R.id.add_main_msg);
            add_msg.setText(Constant.CODE_MSG_Z);
            // 获取手机号
            TextView phone = findViewById(R.id.main_phone);
            new Thread(() -> {
                Constant.CODE_MSG = HttpControllerUtil.getWeChatMiniProgramCode(phone.getText().toString());
                Constant.handler.post(() -> {
                    add_msg.setText(Constant.CODE_MSG);
                });
            }).start();

        }
        if (R.id.main_getToken == id) {
            // 获取手机号
            TextView phone = findViewById(R.id.main_phone);
            String phoneString = phone.getText().toString();
            // 获取消息提示
            TextView add_msg = findViewById(R.id.add_main_msg);

            // 获取验证码
            TextView core = findViewById(R.id.main_core);
            String coreString = core.getText().toString();
            new Thread(() -> {
                Constant.CODE_DATA = HttpControllerUtil.getWeChatMiniProgramToken(phoneString, coreString);
                Constant.handler.post(() -> {
                    Constant.handler.post(()->{
                        add_msg.setText(Constant.TOKEN_TQ);
                    });
                });
                if (Constant.CODE_DATA == null) {
                    Constant.handler.post(()->{
                        add_msg.setText("获取失败!");
                    });
                } else {
                    JSONObject data = Constant.CODE_DATA;
                    String ticket = data.getString("ticket");
                    String mobile = data.getString("mobile");
                    String token = data.getString("token");
                    // 更新主token
                    Constant.MAIN_TOKEN = token;
                    Constant.MAIN_TICKET = ticket;
                    Constant.MAIN_ACCOUNT_STATUS = "正常";
                    // 插入数据库
                    TokenObject tokenObject = new TokenObject(token, ticket, mobile, "正常", 0, "0", Constant.USER_ACCOUNT);
                    Constant.SON_TOKENS.add(tokenObject);
                    // 插入数据库
                    boolean b = MyHttpUtil.insertUserToken(tokenObject);
                    if (b) {
                        Constant.handler.post(() -> {
                            add_msg.setText("设置主号成功");
                            DialogUtil.showDialog(Constant.context, "设置主号成功");
                        });
                    } else {
                        add_msg.setText("设置主号失败");
                        DialogUtil.showDialog(Constant.context, "设置主号失败");
                    }
                }
            }).start();

        }
        if (R.id.add_main_return == id) {
            Intent intent = new Intent();
            intent.setClass(MyMainToken.this, LoginActivity.class);
            startActivity(intent);
        }
    }

}